<?php

namespace app\api\controller;

use app\api\controller\Tpmecms;
use think\Db;
/**
 * 私人定制📂
 * 
 */
class Need extends Tpmecms
{


    // 无需登录的接口,*表示全部
    protected $noNeedLogin = ['p'];
    // 无需鉴权的接口,*表示全部
    protected $noNeedRight = ['*'];


    /**
     * 添加需求
     * @ApiSummary ("0=待处理1=未联系上2=已联系上3=预定成功4=暂无需求5=完结")
     * @return void
     */
    public function add_need()
    {
        $params = $this->request->param();
        // halt($params);
        $fields = ['name','tel','wechat','email','price','cityaddr','school','goodscate_ids','need','time_s'];
        $data = $this->get_field_arr($params,$fields);
        // halt($data);

        $data['uid'] = $this->auth->id;
        $res = Db::name('need')->where($data)->find();
        
        if($res){
            $this->error('已有该需求');
        }else{
            $data['status'] = '0';
            $data['time'] = time();
            $id = Db::name('need')->insertGetId($data);

            if($id)$this->success('提交成功');

        }
    }

    /**
     * 获取提交的私人定制需求列表
     *
     * @param integer $page 1
     * @param integer $limit 10
     * @param int $status null全部0=待处理1=未联系上2=已联系上3=预定成功4=暂无需求5=完结
     * @return void
     */
    public function get_need_list($page=1,$limit=10,$status=null)
    {
        $data['uid'] = $this->auth->id;
        if($status){
            $data['status'] = $status;
        }

        $list = Db::name('need')
                ->where($data)
                ->order('time desc')
                ->page($page,$limit)
                ->select();
        foreach($list as $key=>&$val){
            $val['goodcate'] = $this->GoodscateIdsToArray($val['goodscate_ids']);
        }
        $this->success('ok',$list);
    }


    /**
     * 获取定制信息
     *
     * @param int $id id
     * @return void
     */
    public function get_need_data($id)
    {
        $data['uid'] = $this->auth->id;
        $data['id'] = $id;
        $res = Db::name('need')
            ->where($data)
            ->find();

        $this->success('ok',$res);
    }


    
}
